package com.example.moneygrosService;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.telephony.PhoneNumberUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import com.example.moneygrosService.database.MoneyContract.UsersColumns;
import com.example.moneygrosService.handlers.ManageContactHandler;
import com.example.moneygrosService.handlers.ResultListener;

public class AddContact extends Activity implements OnClickListener {

	public AddContact() {
		// TODO Auto-generated constructor stub

	}


	/* (non-Javadoc)
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		setContentView(R.layout.add_user_layout);


		// Initialisation des tests pour l'ajout d'utilisateur 
		EditText add_user_user_id_EditText = (EditText)findViewById(R.id.add_user_user_id);
		add_user_user_id_EditText.setOnFocusChangeListener(new OnFocusChangeListener() {
			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				// TODO Auto-generated method stub
				EditText eText = (EditText)v;
				String str=eText.getText().toString();
				if(isCorrectMail(str)){
					eText.setTextColor(Color.BLACK);
				} else {
					eText.setTextColor(Color.RED);
				}
			}

			boolean isCorrectMail (String s){		    
				String expression = "^[\\w\\.-]+@([\\w\\-]+\\.)+[A-Z]{2,4}$";
				CharSequence inputStr = s;

				Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
				Matcher matcher = pattern.matcher(inputStr);
				return matcher.matches();
			}
		});
		EditText add_user_user_phone_EditText = (EditText)findViewById(R.id.add_user_user_phone);
		add_user_user_phone_EditText.setOnFocusChangeListener(new OnFocusChangeListener() {
			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				// TODO Auto-generated method stub
				EditText eText = (EditText)v;
				String str=eText.getText().toString();
				if(PhoneNumberUtils.isGlobalPhoneNumber(str)){
					eText.setTextColor(Color.BLACK);
				} else {
					eText.setTextColor(Color.RED);
				}
			}
		});

		EditText add_user_user_bluetooth_mac_EditText = (EditText)findViewById(R.id.add_user_user_bluetooth_mac);
		add_user_user_bluetooth_mac_EditText.setOnFocusChangeListener(new OnFocusChangeListener() {
			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				// TODO Auto-generated method stub
				EditText eText = (EditText)v;
				String str=eText.getText().toString();
				if(isCorrectBtMAC(str)){
					eText.setTextColor(Color.BLACK);
				} else {
					eText.setTextColor(Color.RED);
				}
			}

			boolean isCorrectBtMAC(String s){		    
//				String expression = "^[\\w\\.-]+@([\\w\\-]+\\.)+[A-Z]{2,4}$";
				String expression = "^([0-9A-F]{2}[:-]){5}([0-9A-F]{2})$";
				CharSequence inputStr = s;

				Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
				Matcher matcher = pattern.matcher(inputStr);
				return matcher.matches();
			}
		});


		Button add_user_Button = (Button) findViewById(R.id.add_user_add_button);
		add_user_Button.setOnClickListener(new ButtonClickListener(this));
	}


	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub

	}

	public class ButtonClickListener implements OnClickListener, ResultListener{
		private Activity mAct;
		public ButtonClickListener (Activity act){
			mAct = act;    		
		}

		public void onClick(View v) {
			//				User user = new User();
			EditText user_id = (EditText)findViewById(R.id.add_user_user_id);
			EditText user_name= (EditText)findViewById(R.id.add_user_user_name);
			EditText user_phone = (EditText)findViewById(R.id.add_user_user_phone);
			EditText bluetooth_mac = (EditText)findViewById(R.id.add_user_user_bluetooth_mac);
			CheckBox bidirectional= (CheckBox)findViewById(R.id.add_user_bidirectional);

			Bundle bundle = new Bundle();
			bundle.putString(UsersColumns.USER_ID, user_id.getText().toString());
			bundle.putString(UsersColumns.USER_NAME, user_name.getText().toString());
			bundle.putString(UsersColumns.PHONE_NUMBER, user_phone.getText().toString());
			bundle.putString(UsersColumns.BLUETOOTH_MAC, bluetooth_mac.getText().toString());
			bundle.putBoolean(UsersColumns.BIDIRECTIONAL, bidirectional.isChecked());
			bundle.putString(UsersColumns.VERSION, "1");
			setResult(RESULT_OK, getIntent().putExtras(bundle));
			//			Toast.makeText(getApplicationContext(), "bundle: "+bundle.toString(), Toast.LENGTH_LONG).show();
			ManageContactHandler addContactHandler = new ManageContactHandler(getContentResolver(), bundle);
			//				ManageContactHandler addContactHandler = new ManageContactHandler(getContentResolver(), user);
			addContactHandler.manageUser();
			Button add_user_Button = ((Button) findViewById(R.id.add_user_add_button));
			add_user_Button.setEnabled(false);
			

			addContactHandler.addResultListener(this);

		}

		@Override
		public void onResult(boolean result, int type, Object extra) {
			//faire des tests ici
			if(result){
				mAct.finish();
			} else {
				String msg = "Something went wrong";
				if(type==ResultType.EXISTING_USER){
					msg = "Exisiting user";
				}

				Toast.makeText(mAct, msg, Toast.LENGTH_SHORT).show();
    			Button add_user_Button = ((Button) findViewById(R.id.add_user_add_button));
    			add_user_Button.setEnabled(true);
			}
		}
	}
}
